package com.vviton.zyhj.common.utils;

import java.util.Random;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * ClassName: ObjectUtils <br/>
 * Function: ObjectUtils工具类. <br/>
 * Date: 2016年3月11日 上午11:30:50 <br/>
 *
 * @author qiaocf
 * @version 1.0
 * @since JDK 1.7
 */
public abstract class ObjectUtils {

    /**
     *
     * getRandomPwd:随机生成N位数字字符串
     *
     * @param num 位数
     * @return String
     */
    public static String getRandomPwd(int num) {
        Random random = new Random();
        String result = "";
        for (int i = 0; i < num; i++) {
            result += random.nextInt(10);
        }
        return result;
    }

    /**
     *
     * getIpAddr:获取客户端IP地址
     *
     * @param request 请求
     * @return String
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
